timer on 1
use 		"$datapath/A3_gen.dta",clear
gen eventtime_ankomst = date - ym(year(ankomst_dat), month(ankomst_dat)) 
*order lopnr date firmid eventtime_def atfirm otherfirm jobsinamonth annual_ear_atfirm annual_ear_otherfirm E1 Eear1 E2 Eear2 E3 Eear3 E4 Eear4 monthly_w1 monthly_w2
 
 
keep  E*1 E*2 ankomst_dat persid lopnr year eventtime_ankomst

forvalues i=1(1)2{
	rename E`i' E`i'_ankomst
	rename Eear`i' Eear`i'_ankomst
}
// Get rid of employers who they worked for prior to notification 
*===============================================================================
*Firmid for employers held in a 12 month period before notification

forvalues i=1(1)2{
	qui gen xpreE`i' = E`i'_ankomst if inrange(eventtime_ankomst,-12,-1)
	qui bys lopnr (eventtime_ankomst) : egen preE`i'_ankomst=max(xpreE`i')
	drop xpreE
}

forvalues i=1(1)2{
	forvalues j=1(1)2{
		qui replace E`i'_ankomst = . if preE`j'_ankomst==E`i'_ankomst
	}
}	

// Get rid of att additional employers who have average monthly earnings below X SEK 
*===============================================================================
*Note: 	Employer ID:s E1-E10 are sorted such that highest annual earnings (among
*	the "additional" employers) correpsond to E1 and then in decending order

*Set minimum earnings threshold
local X = 10000
forvalues i = 1(1)2{
	qui replace E`i'_ankomst=. if Eear`i'_ankomst<`X'  
}



{ // Generate newfirm id and employment marker
*===============================================================================
gen newfirmid_ankomst = .
forvalues i=1(1)2{
	qui replace newfirmid_ankomst = E`i'_ankomst if newfirmid_ankomst==. & eventtime_ankomst>=0
}	



gen newfirm_ankomst = newfirmid_ankomst!=. 

bys lopnr (eventtime_ankomst) : gen nvals = sum(newfirmid_ankomst!=.)
gen xfirstnewfirmid = newfirmid_ankomst if nvals==1
bys lopnr (eventtime_ankomst) : egen firstnewfirmid_ankomst = max(xfirstnewfirmid)
drop nvals xfirstnewfirmid
}
*
*order lopnr date firmid eventtime_def atfirm otherfirm working newfirm jobsinamonth annual_ear annual_ear_atfirm annual_ear_otherfirm firstnewfirmid newfirmid E*


{ // Merge on wages for new firms 
*===============================================================================
*Merge on wages for all(!) new jobs
preserve
	use  "$datapath/A1_clean_wagesurvey_00_19.dta",clear
	rename firmid newfirmid_ankomst
	keep persid year newfirmid_ankomst manl 
	foreach var of varlist manl {
		rename `var' 	`var'_newfirm_ankomst
	}	
	tempfile temp
	save 	`temp'
restore	

merge m:1 persid newfirmid_ankomst year using `temp', keepusing(*newfirm_ankomst)
drop if _merge==2
drop _merge

*Merge on wage for first(!) new job (maybe unnessecary?)
preserve
	use  "$datapath/A1_clean_wagesurvey_00_19.dta",clear
	rename firmid firstnewfirmid_ankomst
	keep persid year firstnewfirmid_ankomst manl
	rename manl manl_firstnewfirmid_ankomst
	tempfile temp
	save 	`temp'
restore	

fmerge m:1 persid firstnewfirmid_ankomst year using `temp', keepusing(manl_firstnewfirmid_ankomst)
drop if _merge==2
drop _merge
}
*



{ // Generate new employment markers
*===============================================================================
gen otherfirm2_ankomst = newfirm_ankomst


}
*








keep lopnr persid year eventtime_ankomst otherfirm2_ankomst E* manl* *newfirm*
drop Esni*

compress
save "$datapath/A3_gen_empmark_ankomst.dta",replace

